package com.cjbdi.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.cjbdi.util.NumUtil;


/**
 * FpxszsService           
 */
@Service
@Transactional
public class SpxgFpxszsService{
	@Autowired  
    @PersistenceContext  
    private EntityManager entityManager;// 注入EntityManager实例 
	@Autowired
	BaseService baseService;
	//服判息诉指数概述（和去年同比） 
	public List getGsList(String kind,String type, String date, String fybm, String casetype){  
    	Query query = entityManager.createNativeQuery("select date,fymc,jbfy,glnum,totalnum,result from sfzs_result_fpxs where type='"+type+"' and date in('"+date+"','"+(Integer.parseInt(date)-1)+"') and kind='"+kind+"' and jbfy='"+fybm+"' and casetype='"+casetype+"' order by date desc");  
        query.unwrap(SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);  
        List rows = query.getResultList();  
        return rows;
    }
	//附表表格
    public List getFbList(String type, String date, String fybm, String kind, String casetype){ 
    	StringBuffer sql = new StringBuffer("select fyjc,glnum,totalnum,result from sfzs_result_fpxs,sfzs_czrk where sfzs_result_fpxs.jbfy=sfzs_czrk.fybm and type='"+type+"' and date ='"+date+"' and sfzs_czrk.year='"+date+"' and kind='"+kind+"' and ");
    	if(kind.equals("高")) {
    		sql.append("jbfy not in('X00','000','ZZ0')");
    	}else {
    		sql.append("jbfy like'"+fybm.substring(0, 1)+"%'");
    	}
    	sql.append("and casetype='"+casetype+"' order by result desc");
    	Query query = entityManager.createNativeQuery(sql.toString());  
        query.unwrap(SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);  
        List rows = query.getResultList();  
        for(int i=0;i<rows.size();i++) {
			Map row = (Map)rows.get(i);
			row.put("id", i+1);
			row.put("lv", NumUtil.toNxiaoshu(row.get("result").toString(), 2)+"%");
		}
        return rows;
    }
    //全国排名表格
    /*public List getQgTableList(String type, String date, boolean isQuanguo){ 
    	StringBuffer sql = new StringBuffer("select jbfy,fy,lastyearzs,yearzs,cz,rank() over(order by yearzs desc) as rank from(SELECT jbfy,fymc as fy,\r\n" + 
    			"sum(case when date='"+(Integer.parseInt(date)-1)+"' then result else 0 end )as lastyearzs,\r\n" + 
    			"sum(case when date='"+date+"' then result else 0 end) as yearzs,\r\n" + 
    			"(sum(case when date='"+date+"' then result else 0 end) - sum(case when date='"+(Integer.parseInt(date)-1)+"' then result else 0 end )) as cz from sfzs_result_fpxs where (jbfy not in('X00','000') or jbfy is null)");
    	if(isQuanguo) {
    		sql.append(" and fymc='全国'");
    	}else {
    		sql.append(" and fymc!='全国'");
    	}
    	sql.append(" and type='"+type+"' and date in('"+date+"','"+(Integer.parseInt(date)-1)+"') and kind ='高' GROUP BY jbfy,fymc)t");
    	Query query = entityManager.createNativeQuery(sql.toString());  
        query.unwrap(SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);  
        List rows = query.getResultList();  
        return rows;
    }*/
    //中院排名表格
    /*public List getZjTableList(String type, String date, String fybm){ 
    	String sql = "SELECT jbfy,fyjc as fy,\r\n" + 
        			"sum(case when date='"+(Integer.parseInt(date)-1)+"' then result else 0 end )as lastyearzs,\r\n" + 
        			"sum(case when date='"+date+"' then result else 0 end) as yearzs,\r\n" + 
        			"(sum(case when date='"+date+"' then result else 0 end) - sum(case when date='"+(Integer.parseInt(date)-1)+"' then result else 0 end )) as cz from sfzs_result_fpxs,sfzs_czrk where sfzs_result_fpxs.jbfy=sfzs_czrk.fybm and type='"+type+"' and sfzs_czrk.year='"+date+"' and kind ='中' and jbfy like'"+fybm.substring(0, 1)+"%' GROUP BY jbfy,fyjc ORDER BY yearzs desc";
    	Query query = entityManager.createNativeQuery(sql);  
        query.unwrap(SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);  
        List rows = query.getResultList();  
        return rows;
    }*/
    //附表表格（区县）
    public List getFbQxList(String type, String date, String fybm, String casetype){  
    	String sql = "select replace(fymc,'人民法院','法院') as fyjc,glnum,totalnum,result from sfzs_result_fpxs where type='"+type+"' and sfzs_result_fpxs.date ='"+date+"' and kind='辖' and jbfy like'"+fybm.substring(0, 2)+"%' and right(fymc,5)<>'（已撤销）' and casetype='"+casetype+"' order by result desc";
    	Query query = entityManager.createNativeQuery(sql);  
        query.unwrap(SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);  
        List rows = query.getResultList();  
        return rows;
    }
    //附表表格（区县本身）
    public List getFbQxSelfList(String type, String date, String fybm, String casetype){  
    	String sql = "select replace(fymc,'人民法院','法院') as fyjc,glnum,totalnum,result from sfzs_result_fpxs where type='"+type+"' and sfzs_result_fpxs.date ='"+date+"' and kind='辖' and jbfy ='"+fybm+"' and casetype='"+casetype+"' order by result desc";
    	Query query = entityManager.createNativeQuery(sql);  
        query.unwrap(SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);  
        List rows = query.getResultList();  
        return rows;
    }
    
    public void getFpxszsGy(Map dataMap,String kind,String type, String date, String fybm ,String fymc, String casetype) {//裁判文书服判息诉指数
    	getSectionOne(dataMap,kind,type,date,fybm,casetype);
		List qgTableList = baseService.getQgTableList("sfzs_result_fpxs",type, date, false, true,casetype);
		List qgSelfList = baseService.getQgTableList("sfzs_result_fpxs",type, date, true, true,casetype);
		List qgTableList3 = new ArrayList();
		Map rowSelf = new HashMap();
		for(int i=qgTableList.size()-1;i>=0;i--) {//为全国报告增加
			Map row = (Map)qgTableList.get(i);
			String fy = row.get("fy").toString();
			if(fy.contains("北京")||fy.contains("天津")||fy.contains("上海")||fy.contains("重庆")) {
				continue;
			}else {
				dataMap.put("spxg_"+casetype+"_rank1_qg",NumUtil.toSheng(fy)+"法院");
				if(fymc.equals("全国")) {
					List zjList = baseService.getZjTableList("sfzs_result_fpxs", type, date, row.get("jbfy").toString(),true, casetype);
					Map rowzj = (Map)zjList.get(0);
					String rankZJ1 = rowzj.get("fy").toString();
					dataMap.put(17,NumUtil.toSheng(fy));
					dataMap.put("spxg_"+casetype+"_rank1_zj", rankZJ1);//中级排名第一
				}
				break;
			}
		}
		baseService.getGyDataIntoMap(dataMap, qgTableList, qgSelfList, qgTableList3, fymc, "spxg_"+casetype);
		List zjTableList = baseService.getZjTableList("sfzs_result_fpxs", type, date, fybm,true,casetype);//当前高院所包含的中院排名列表
		List gylist = baseService.getSelfZs("sfzs_result_fpxs", type, date, fybm, casetype);
		baseService.getZyDetailDataIntoMap(dataMap, zjTableList, gylist, fymc, "spxg_"+casetype);
		
		//处理表格
		if(fymc.equals("全国")) {
			dataMap.put("array8",qgTableList);
			List fbList = getFbList(type,date,fybm,"高",casetype);
			dataMap.put("array43",fbList);
		}else {
			List fbList = getFbList(type,date,fybm,"中",casetype);
			dataMap.put("array15",qgTableList3);
			dataMap.put("array16",zjTableList);
			dataMap.put("array44",fbList);	
		}
		
    }
    public void getFpxszsZy(Map dataMap,String kind, String type, String date, String fybm ,String fymc, String casetype) {//裁判文书公开指数   
    	List fbList = getFbQxList(type,date,fybm,casetype);
    	baseService.handleFb(fbList, fymc);
    	
		// 辖区排名
		List xqTableList = baseService.getXqTableList("sfzs_result_fpxs", type, date, fybm, true, casetype);
		List zylist = baseService.getSelfZs("sfzs_result_fpxs", type, date, fybm, casetype);
		baseService.getQxDetailDataIntoMap(dataMap, xqTableList, zylist, fymc, "spxg_" + casetype);

		List tsTableList = baseService.getTsTableList("sfzs_result_fpxs", type, date, fybm, true, casetype);
		baseService.getTsDataIntoMap(dataMap, tsTableList, fybm, "spxg_" + casetype);
		
    	
    	StringBuffer rankZJ3 = new StringBuffer();//全省前三
		int rank = 0;
		List zjTableList = new ArrayList();
		List tdTableList = new ArrayList();
		
		if(fybm.startsWith("1")||fybm.startsWith("2")||fybm.startsWith("M")||fybm.startsWith("9")) {//同等直辖市中院排名表格		
			getSectionOne(dataMap,"辖",type,date,fybm,casetype);
			//同等直辖市中院排名表格		
			tdTableList = baseService.getTdZxTableList("sfzs_result_fpxs", type, date, fybm,true, casetype);
			baseService.getTdZxDataIntoMap(dataMap, tdTableList, fybm, "spxg_"+casetype);
			zjTableList = baseService.getZjZxTableList("sfzs_result_fpxs", type, date, fybm,true, casetype);
			baseService.getZyDataIntoMap(dataMap, zjTableList, fybm, "spxg_"+casetype);
			dataMap.put("array22",zjTableList);
			dataMap.put("array23",tdTableList);
			dataMap.put("array24",xqTableList);
			dataMap.put("array25",tsTableList);
			dataMap.put("array68",fbList); 
		}else {
			getSectionOne(dataMap,kind,type,date,fybm,casetype);
			//同等规模城市排行
			List infoList = baseService.getQxInfo("sfzs_result_fpxs",type, date, fybm, true,casetype);
			tdTableList = baseService.getTdZjTableList("sfzs_result_fpxs",type, date, fybm, true,casetype);
			baseService.getTdCityDataIntoMap(dataMap, infoList, tdTableList, "spxg_"+casetype,fybm);
			
			zjTableList = baseService.getZjTableList("sfzs_result_fpxs", type, date, fybm,true,casetype);
			baseService.getZyDataIntoMap(dataMap, zjTableList, fybm, "spxg_"+casetype);
			
			//同等收案量排行
			List tdSalTableList = baseService.getTdSalTableList("sfzs_result_fpxs",type, date, fybm, true,casetype);
			baseService.getTdSalDataIntoMap(dataMap, tdSalTableList, fymc,"spxg_"+casetype);
			//处理表格
			dataMap.put("array26",zjTableList);
			dataMap.put("array27",tdTableList);
			dataMap.put("array28",xqTableList);
			dataMap.put("array29",tsTableList);
			dataMap.put("array73",fbList); 
		}
		
	}
    
    public void getQxZs(Map dataMap,String kind, String type, String date, String fybm, String casetype) {//区县法院指数
    	getSectionOne(dataMap,kind,type,date,fybm,casetype);
    	List xqTableList = baseService.getXqTableList("sfzs_result_fpxs",type, date, fybm, true,casetype);
    	List gylist = baseService.getGyOrZyXqZs("sfzs_result_fpxs", type, date, fybm, casetype,"高");
    	List zylist = baseService.getGyOrZyXqZs("sfzs_result_fpxs", type, date, fybm, casetype,"中");
    	baseService.getQxDataIntoMap(dataMap, xqTableList, fybm, "spxg_"+casetype,gylist,zylist);
    	String ssxq_gy_rank = baseService.getRankInGyxq("sfzs_result_fpxs",type,date,fybm,true,casetype).get(0).toString();
    	dataMap.put("spxg_"+casetype+"_rank_gy",  "第" + NumUtil.toChineseNum(ssxq_gy_rank) + "位");
    	List tsTableList = baseService.getTsTableList("sfzs_result_fpxs",type,date,fybm,true,casetype);
    	baseService.getTsDataIntoMap(dataMap, tsTableList, fybm, "spxg_"+casetype);
    	List fbList = getFbQxSelfList(type,date,fybm,casetype);
		for(int i=0;i<fbList.size();i++) {
			Map row = (Map)fbList.get(i);
			row.put("id", i+1);
			row.put("lv", NumUtil.toNxiaoshu(row.get("result").toString(), 2)+"%");
		}
    	dataMap.put("array12",xqTableList);
    	dataMap.put("array13",tsTableList);
    	if(casetype.equals("ysfp")) {
    		dataMap.put("array37",fbList); 
    	}
    	
    }
    
    public void getSectionOne(Map dataMap,String kind,String type, String date, String fybm, String casetype) {
    	List gslist = getGsList(kind,type,date,fybm,casetype);//获取服判息诉指数概况
    	Map mapYear = new HashMap();
    	Map mapLastYear = new HashMap();
    	double zs = 0;
    	double zsLastYear = 0;
    	dataMap.put("spxg_"+casetype+"_totalnum", "--");//一审审结案件
		dataMap.put("spxg_"+casetype+"_glnum", "--");//二审新收案
		dataMap.put("spxg_"+casetype, "--");
    	if(gslist.size()>1) {
    		mapYear = (Map)gslist.get(0);
        	mapLastYear = (Map)gslist.get(1);
        	zs = NumUtil.toNxiaoshu(mapYear.get("result").toString(), 2);
        	zsLastYear = NumUtil.toNxiaoshu(mapLastYear.get("result").toString(), 2);    
        	dataMap.put("spxg_"+casetype+"_totalnum", NumUtil.toWan(Integer.parseInt(mapYear.get("totalnum").toString())));//一审审结案件
    		dataMap.put("spxg_"+casetype+"_glnum", NumUtil.toWan(Integer.parseInt(mapYear.get("glnum").toString())));//二审新收案
    		dataMap.put("spxg_"+casetype, zs);
    	}
		if(zs>zsLastYear) {
			dataMap.put("spxg_"+casetype+"_tb", "上升"+(NumUtil.toNxiaoshu(zs-zsLastYear,2)));//同比
		}else if(zs<zsLastYear) {
			dataMap.put("spxg_"+casetype+"_tb", "下降"+(NumUtil.toNxiaoshu(zsLastYear-zs,2)));//同比
		}else {
			dataMap.put("spxg_"+casetype+"_tb", "不变");//同比
		}
		if(kind.equals("中")) {
			Map zyxq = new HashMap();
			zyxq.put("fy", dataMap.get("fy")+"法院");
			zyxq.put("lastyearzs",zs);
			zyxq.put("yearzs",zsLastYear);
			zyxq.put("cz",NumUtil.toNxiaoshu(zs-zsLastYear,2));
			dataMap.put("zyxq", zyxq);
		}
    }
   
}
